package com.snbc.util;

import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;

public class DirectoryGenerate {
	private static Properties pro= PropertiesUtil.getPro();
	private static int date_gap= Integer.parseInt(pro.getProperty("mr.date.gap", "20"));
	private static SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
	public static void main(String[] args) throws ParseException {
		System.out.println(DirectoryGenerate.getLastSubDir("20190724"));
		System.out.println("201907240072".compareTo("201907240072"));
		
	}
	
	/**
	 * 获得指定时间对应目录的上一个目录
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getDateDirBefore(Date date) throws ParseException {
		String currentDir=getDateDir(date);
		return getDateDirBefore(currentDir);
	}
	/**
	 * 获得指定目录名的上一个目录
	 * @param dirName
	 * @return
	 * @throws ParseException
	 */
	public static String getDateDirBefore(String dirName) throws ParseException {
		String dir = "";
		String theDay_begin_str=dirName.substring(0, 8);
		String dev=dirName.substring(8);
		int minute_dis= Integer.parseInt(dev);
		if(minute_dis>1){
			dir=theDay_begin_str+getPathSuffix(minute_dis-1);
		}else{
			String year=theDay_begin_str.substring(0,4);
			String month=theDay_begin_str.substring(4, 6);
			String day=theDay_begin_str.substring(6);
			Calendar cal =  new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(day),0,0,0);
			
			cal.add(Calendar.DAY_OF_MONTH, -1);
			dir=sdf.format(cal.getTime());
			dir+=getPathSuffix(((60*24-1)/date_gap+1));
		}
		return dir;
	}
	public static String getCurrentDir() throws ParseException {
		return getDateDir(new Date());
	}
	
	/**
	 * 获得指定时间对应目录
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getDateDir(Date date) throws ParseException {
		String snd_path_prefix=sdf.format(date);
		Date date_begin=new SimpleDateFormat("yyyyMMddHHmmss").parse(snd_path_prefix+"000000");
		long minute_dis=(date.getTime()-date_begin.getTime())/(60*1000);
		long dev=minute_dis/date_gap+1;
		return snd_path_prefix+getPathSuffix((int)dev);
	}
	/**根据目录名获得对应的时间
	 * dirName
	 * @param dirName
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(String dirName) throws ParseException {
		String theDay_begin_str=dirName.substring(0, 8);
		String dev=dirName.substring(8);
		long minu_dis= Integer.parseInt(dev)*date_gap;
		minu_dis=minu_dis==0?date_gap:minu_dis;
		minu_dis=minu_dis>24*60?24*60:minu_dis;
		long hour=minu_dis/60;
		long minute=minu_dis%60;
		if(minute-date_gap/2<0){
			hour=hour-1;
			minute=60+(minute-date_gap/2);
		}else{
			minute=minute-date_gap/2;
		}
		String hour_str=hour<10?"0"+hour:""+hour;
		String minute_str=minute<10?"0"+minute:""+minute;
		
		Date theDay=new SimpleDateFormat("yyyyMMddHHmmss").parse(theDay_begin_str+hour_str+minute_str+"00");
		return theDay;
	}
	
	
	/**
	 * 获得指定时间对应目录的下一个目录
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getDateDirNext(Date date) throws ParseException {
		String currentDir=getDateDir(date);
		return getDateDirNext(currentDir);
	}
	/**
	 * 获得指定目录名的下一个目录
	 * @param dirName 目录格式必须由12位连续字串构成，且前面8位符合yyyyMMdd格式，后面4位为数字，例如"201603240001"
	 * @return
	 * @throws ParseException
	 */
	public static String getDateDirNext(String dirName) throws ParseException {
		String dir = "";
		String suffix = "000";
		String dev=dirName.substring(dirName.length()-2);
		int index= Integer.parseInt(dev);
		if(index>=72){
		String theDay_begin_str=dirName.substring(dirName.length()-12);
			String year=theDay_begin_str.substring(0,4);
			String month=theDay_begin_str.substring(4, 6);
			String day=theDay_begin_str.substring(6,8);
			Calendar cal =  new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(day),0,0,0);
			cal.add(Calendar.DAY_OF_MONTH, 1);
			
			dir=sdf.format(cal.getTime());
			dir = "/fsnfile/"+dir+"/"+dir+"0001";
			
		}else{
			index++;
			suffix = suffix+index;
			suffix = suffix.substring(suffix.length()-4);
			dir = dirName.substring(0,dirName.length()-4)+suffix;
		}
		return dir;
	}
	
	
	public static String getTodayDir(){
		return sdf.format(new Date());
	}
	
	/**
	 * 20160325
	 * @param str
	 * @return
	 */
	public static String dayDirIncre(String str){
		String year=str.substring(0, 4);
		String mon=str.substring(4, 6);
		String day=str.substring(6);
		Calendar cal =  new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(mon)-1, Integer.parseInt(day),0,0,0);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		return sdf.format(cal.getTime());
	}
	public static String getFistSubDir(FileSystem fs, String fsnfilepath){
		Path path = new Path(fsnfilepath);
		FileStatus[] fstatus;
		String name = null;
		try {
			if(fs.exists(path)){
				fstatus = fs.listStatus(path);
				if (fstatus != null && fstatus.length > 0)
					for(FileStatus fileStatus:fstatus) {
						FileStatus first = fileStatus;
						if(name==null) {
							name = first.getPath().getName();
							continue;
						}
						if(name.compareTo(first.getPath().getName())>0) {
							name = first.getPath().getName();
						}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return name;
	}
	public static String getLastSubDir(String dayDir){
		long minute=60*24;
		long subdirIndex=minute%date_gap==0?minute/date_gap:minute/date_gap+1;
		return dayDir+getPathSuffix((int)subdirIndex);
	}
	/**
	 * 60*24=1440
	 * @param minuteNum
	 * @return
	 */
	private static String getPathSuffix(int minuteNum){
		int suffixLen = 4;
        int len = String.valueOf(minuteNum).length();
        StringBuilder suffix = new StringBuilder();

        for(int i = 0; i < suffixLen - len; ++i) {
            suffix.append("0");
        }
        suffix.append(minuteNum);
        return suffix.toString();
	}
	
}
